package edu.objects;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class PositionTile extends Tile {
	private int _r;
	private int _c;
	
	public PositionTile(char value, boolean blankTile) {
		super(value, blankTile);
	}
	
	/**
	 * Parses a tile as sent by the client. This string may be, for example:
	 * "H7,7" - A plain H in the middle of the board.
	 * "_I8,7" - A blank, acting as an I, near the last one. HI
	 * @param value
	 */
	public PositionTile(String value) {
		super(getLtr(value),isblank(value));
		Pattern p = Pattern.compile("\\_?[A-Z](\\d*),(\\d*)");
		Matcher m = p.matcher(value);
		if (m.find()) {
			setPosition(Integer.valueOf(m.group(1)), Integer.valueOf(m.group(2)));
		    //System.out.println(m.group(1)+"..."+m.group(2));
		}

		//Silly Java insisting on constructor first ...
		/*boolean blank = false;
		if (value.charAt(0)=='_') {
			blank = true;
			value = value.substring(1);
		}
		char letter = value.charAt(0);
		value = value.substring(1);*/
		
		
	}
	
	private static boolean isblank(String value) {
		return value.charAt(0)=='_';
	}

	private static char getLtr(String value) {
		if (isblank(value)) {
			return value.charAt(1);
		} else {
			return value.charAt(0);
		}
	}

	public int getR() {
		return _r;
	}
	
	public int getC() {
		return _c;
	}
	
	public int getValue() {
		return getValue();
	}
	
	public void setPosition(int r, int c) {
		_r = r;
		_c = c;
	}
	
	public String toString() {
		return _r+","+_c+","+Character.toString(getLetter());
	}


}
